package com.coda.clouddisk.service.impl;

import com.coda.clouddisk.context.BaseContext;
import com.coda.clouddisk.entity.*;
import com.coda.clouddisk.exception.BaseException;
import com.coda.clouddisk.mapper.FileCategoryMapper;
import com.coda.clouddisk.mapper.FileShareRecordMapper;
import com.coda.clouddisk.mapper.FiletofolderrecordMapper;
import com.coda.clouddisk.mapper.SuperMapper;
import com.coda.clouddisk.message.MessageContext;
import com.coda.clouddisk.service.SuperService;
import com.coda.clouddisk.service.inter.FileCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class FileCategoryServiceImpl extends SuperService implements FileCategoryService {

    @Autowired
    private FileCategoryMapper fileCategoryMapper;
    @Autowired
    private FiletofolderrecordMapper filetofolderrecordMapper;

    @Override
    public SuperMapper getMapper() {
        return fileCategoryMapper;
    }

    /**
     * 以树状结构查询文件分类
     */
    @Override
    public KesunReturn findAll(Map<String, Object> data) {
        KesunReturn kesunReturn = super.findAll(data);
        ArrayList<FileCategory> fileCategoryList = (ArrayList<FileCategory>) kesunReturn.getObj();
        ArrayList<FileCategory> fileCategoryTree = getFileCategoryTree(fileCategoryList,"0");
        kesunReturn.setObj(fileCategoryTree);
        return kesunReturn;

    }

    /**
     * 递归获取
     * @param fileCategoryList
     * @param pid
     * @return
     */
    private ArrayList<FileCategory> getFileCategoryTree(ArrayList<FileCategory> fileCategoryList, String pid) {

        //用于存储树形结构的集合
        ArrayList<FileCategory> ListTree = new ArrayList<>();

        //遍历所有类型
        for (FileCategory fileCategory : fileCategoryList) {
            if (fileCategory.getParent().equals(pid)){
                ListTree.add(fileCategory);
            }
        }

        //遍历存储树形结果的集合
        for (FileCategory fileCategory : ListTree) {
            ArrayList<FileCategory> fileCategoryTree = getFileCategoryTree(fileCategoryList, fileCategory.getId());
            fileCategory.setChildCategory(fileCategoryTree);
        }
        return ListTree;
    }

    /**
     * 判断分类名称是否重复
     * @return
     */
    @Override
    public KesunReturn edit() {
        Map<String, Object> models = super.getModels();
//        User user = new User();
//        user.setId(BaseContext.getCurrentId());
//        models.put("user",user);
        //查询是否有重名文件
        int count = fileCategoryMapper.findCategoryById(models);
            if (count >0){
            throw new BaseException(MessageContext.ERROR_CODE,"分类名称重复");
        }
        return super.edit();
    }

    /**
     * 删除文件分类
     */
    @Override
    public KesunReturn delete(ArrayList<String> ids) {
        for (String id : ids) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("category",id);

            //查询分类下是否有文件
            int count = filetofolderrecordMapper.getFileToCategory(map);
            if (count > 0){
                throw new BaseException(MessageContext.ERROR_CODE,"请先删除分类下的文件");
            }
            //判断该分类是否可以删除

            User user = new User();
            user.setId(BaseContext.getCurrentId());
            ids.forEach(i -> {
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("id",i);
                map1.put("user",user);
                FileCategory  fileCategory = (FileCategory) fileCategoryMapper.find(map1);
                if ((fileCategory.getCanDeleted())){
                    throw new BaseException(MessageContext.ERROR_CODE,"该文件夹不能删除");
                }
            });

        }
        return super.delete(ids);
    }

    @Override
    public KesunReturn add() {

        Map<String, Object> models = super.getModels();
        //判断文件分类名是否重复
        int count = fileCategoryMapper.findCategoryById(models);
        if (count > 0){
            throw new BaseException(MessageContext.ERROR_CODE,"该分类名已存在");
        }
        return super.add();
    }
}
